let data = {};
let editingId = null;
let requestUrl = null;

function addHandler() {
	let list = $("#view_attribute_list");

	list.datagrid("appendRow",{
		equipmentId: null,
		propertyId: null,
		grade: 65,
		min: 0,
		max: 0
	});

	let total = list.datagrid("getRows").length;

	editingId = total - 1;
	requestUrl = "/children_equipment_view_attribute/insert";

	list.datagrid("beginEdit", editingId);
}

function editHandler() {
	let list = $("#view_attribute_list");

	if (editingId != null) {
		list.datagrid("selectRow", editingId);
	} else {
		let row = list.datagrid("getSelected");

		if (row) {
			// 获取行索引，这个索引从0开始
			let rowIndex = list.datagrid("getRowIndex", row);

			editingId = rowIndex;
			requestUrl = "/children_equipment_view_attribute/updateById";

			list.datagrid("beginEdit", rowIndex);
		} else {
			alertMsg("请选择要修改的记录！", "warning");
		}
	}
}

function saveHandler() {
	if (editingId != null) {
		// 只有结束编辑才能获取到最新的值
		$("#view_attribute_list").datagrid("endEdit", editingId);

		ajaxPost(requestUrl, data, function(resp) {
			editingId = null;

			showMsg(resp.message);

			$("#view_attribute_list").datagrid("reload");
		}, error);
	}
}

function cancelHandler() {
	if (editingId != null) {
		$("#view_attribute_list").datagrid("cancelEdit", editingId);

		editingId = null;
	}
}

function deleteHandler() {
	let rowData = $("#view_attribute_list").datagrid("getSelected");

	if(rowData) {
		$.messager.confirm("提示", "是否确认删除？", function(bool) {
			if (bool) {
				ajaxPost("/children_equipment_view_attribute/deleteById", {
					id: rowData.id
				}, function(resp) {
					showMsg(resp.message);

					$("#view_attribute_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#equipment_id").combobox({
		url: "/children_equipment_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-装备类型-",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#_grade_").numberspinner({
		min: 65,
		max: 105,
		increment: 20,
		prompt: "输入装备等级"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let equipmentId = $("#equipment_id").combobox("getValue");
		let grade = $("#_grade_").numberspinner("getValue");

		$("#view_attribute_list").datagrid("reload", {
			equipmentId: equipmentId,
			grade: grade
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#view_attribute_list").datagrid({
		url: "/children_equipment_view_attribute/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		fitColumns: true,
		pagination: true,
		onAfterEdit: function (rowIndex, rowData, changes) { // 结束行内编辑事件
			data = {
				id: rowData.id,
				equipmentId: rowData.equipmentId,
				min: changes.min ? changes.min : rowData.min,
				max: changes.max ? changes.max : rowData.max,
				grade: changes.grade ? changes.grade : rowData.grade,
				propertyId: changes.propertyId ? changes.propertyId : rowData.propertyId
			};
		},
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				saveHandler();
			}
		}, "-", {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				cancelHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "propertyId", title: "属性", align: "center", width: 150
				, editor: {
					type: "combobox",
					options: {
						url: "/property/selectByType?type=1",
						valueField: "id",
						textField: "name",
						panelHeight: "auto"
					}
				}
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/property/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#propperty_" + rowIndex).html(data.name);
						}, error);

						return "<span class='blue' id='propperty_" + rowIndex + "'></span>";
					}

					return "/";
				}
			},
			{field: "min", title: "最小属性值", align: "center", width: 150
				, editor: {
					type: "numberspinner",
					options: {
						min: 1
					}
				}
			},
			{field: "max", title: "最大属性值", align: "center", width: 150
				, editor: {
					type: "numberspinner",
					options: {
						min: 1
					}
				}
			},
			{field: "grade", title: "装备等级", align: "center", width: 150
				, editor: {
					type: "numberspinner",
					options: {
						min: 65,
						max: 105,
						increment: 20
					}
				}
			},
			{field: "equipmentId", title: "装备类型", align: "center", width: 150
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/children_equipment_type/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#equipment_" + rowIndex).attr({
								"alt": data.name,
								"src": data.image,
								"title": data.name
							});
						}, error);

						return "<img height='" + size + "' id='equipment_" + rowIndex + "' />";
					}

					return "/";
				}
			}
		]]
	});

});